package com.mywang.weitravel.modules.register;

import com.mywang.weitravel.base.BasePresenter;
import com.mywang.weitravel.model.base.BaseResponse;
import com.mywang.weitravel.net.RetrofitHelper;
import com.mywang.weitravel.net.RxSchedulers;
import com.mywang.weitravel.util.EncodeUtils;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {
    public RegisterPresenter(RegisterContract.View view) {
        super(view);
    }

    @Override
    public void doRegister(String username, String password, String confirm_password) {
        mView.showLoading();
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", EncodeUtils.md5(password));
        params.put("confirm_password", EncodeUtils.md5(confirm_password));

        RetrofitHelper.getInstance().getServer()
                .doRegister(params)
                .compose(RxSchedulers.applySchedulers())
                .as(bindToLife())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        mView.registerSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mView.hideLoading();
                    }
                });
    }
}
